part of '../repository.dart';

const _kLearningLexicalItemProgressLearningStageKey = 'learning_stages';

const _kLearningLexicalItemProgressTypeValue = 'learning';

class _LearningLexicalItemProgressDto extends Equatable implements _LexicalItemProgressDto {
  const _LearningLexicalItemProgressDto._internal({
    required this.learningStageDtos,
  });

  final BuiltList<_LearningStageDto> learningStageDtos;

  @override
  String get type => _kLearningLexicalItemProgressTypeValue;

  @override
  List<Object?> get props {
    return [
      learningStageDtos,
    ];
  }

  factory _LearningLexicalItemProgressDto.fromJson(Map<String, dynamic> json) {
    final stageDtoMaps =
        List.castFrom<dynamic, Map>(json[_kLearningLexicalItemProgressLearningStageKey]);

    final stageDtos = stageDtoMaps.map((stageDtoMap) {
      final stageDtoJson = Map.castFrom<dynamic, dynamic, String, dynamic>(stageDtoMap);
      return _LearningStageDtoFactory().fromJson(stageDtoJson);
    }).toBuiltList();

    return _LearningLexicalItemProgressDto._internal(
      learningStageDtos: stageDtos,
    );
  }

  factory _LearningLexicalItemProgressDto.fromEntity(LearningLexicalItemProgress progress) {
    final stageDtos = progress.learningStages.map((stage) {
      return _LearningStageDtoFactory().fromEntity(stage);
    }).toBuiltList();

    return _LearningLexicalItemProgressDto._internal(
      learningStageDtos: stageDtos,
    );
  }

  @override
  Map<String, dynamic> toJson() {
    final stageJsons = learningStageDtos.map((stageDto) {
      return stageDto.toJson();
    }).toList();

    return {
      _kLexicalItemProgressTypeKey: type,
      _kLearningLexicalItemProgressLearningStageKey: stageJsons,
    };
  }

  @override
  LearningLexicalItemProgress toEntity() {
    final stages = learningStageDtos.map((stageDto) {
      return stageDto.toEntity();
    }).toBuiltList();

    return LearningLexicalItemProgress(
      learningStages: stages,
    );
  }
}
